package com.jk.vehicle.mini.components.impl;

import com.jk.vehicle.mini.components.WxService;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class WxServiceImpl implements WxService {
    @Resource
    private RestTemplate restTemplate;

    /**
     * 小程序-根据微信登陆code获取用户openId等
     * @param code 微信前端生成的jsCode
     * @return 结果
     */
    @Override
    public String loginApplets(String code, String appId, String secret) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", secret);
        paramMap.put("js_code", code);
        return restTemplate.getForObject("https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={js_code}&grant_type=authorization_code",
                String.class, paramMap);
    }
}
